#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-12-04 9:03 上午
# @Author  : yangdy
# @File    : 2020-12-04.659.分割数组为连续子序列
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def isPossible(self, nums: List[int]) -> bool:
        # m = {}
        # for x in nums:
        #     if que := m.get(x-1):
        #         ll = heapq.heappop(que)
        #         if not m.get(x):
        #             m[x] = []
        #         heapq.heappush(m[x], ll+1)
        #     else:
        #         if not m.get(x):
        #             m[x] = []
        #         heapq.heappush(m[x], 1)
        # return all(not que or que[0] >= 3 for que in m.values())
        m = collections.defaultdict(list)
        for x in nums:
            if heap := m.get(x-1):
                ll = heapq.heappop(heap)
                heapq.heappush(m[x], ll+1)
            else:
                heapq.heappush(m[x], 1)
        return all(not h or h[0] >= 3 for h in m.values())


questions = [
    ([1, 2, 3, 3, 4, 5],),
    ([1, 2, 3, 3, 4, 4, 5, 5],),
    ([1, 2, 3, 4, 4, 5],),
]

work(questions, lambda q: Solution().isPossible(q[0]))


